<?php

/**
 * This is the model class for table "msgg_item_hook".
 *
 * The followings are the available columns in table 'msgg_item_hook':
 * @property string $id
 * @property string $iid
 * @property string $lid
 * @property string $time
 * @property string $params
 */
class MsggItemHook extends CActiveRecord
{
    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return 'msgg_item_hook';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        return array(
            array('lid,iid, time', 'length', 'max' => 11),
            array('params', 'safe'),
            array('id, lid,iid, time, params', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        return array();
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'id' => 'ID',
            'lid' => 'LID',
            'iid' => 'Iid',
            'time' => 'Time',
            'params' => 'Params',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     *
     * Typical usecase:
     * - Initialize the model fields with values from filter form.
     * - Execute this method to get CActiveDataProvider instance which will filter
     * models according to data in model fields.
     * - Pass data provider to CGridView, CListView or any similar widget.
     *
     * @return CActiveDataProvider the data provider that can return the models
     * based on the search/filter conditions.
     */
    public function search()
    {
        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id, true);
        $criteria->compare('lid', $this->lid, true);
        $criteria->compare('iid', $this->iid, true);
        $criteria->compare('time', $this->time, true);
        $criteria->compare('params', $this->params, true);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

    /**
     * Returns the static model of the specified AR class.
     * Please note that you should have this exact method in all your CActiveRecord descendants!
     * @param string $className active record class name.
     * @return MsggItemHook the static model class
     */
    public static function model($className = __CLASS__)
    {
        return parent::model($className);
    }

    public static function add($iid,$lid, $params)
    {
        $hook = new self();
        $hook->lid = $lid;
        $hook->iid = $iid;
        $hook->params = json_encode($params);
        $hook->time = time();
        return $hook->save();
    }
}
